package com.dhcc.sdc.api.controller;

import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.entity.DataReceiveLogEntity;
import com.dhcc.cdc.sys.service.ICdcDataReceiveLogService;
import com.dhcc.core.api.controller.BaseApiController;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.modules.msgcenter.entity.MsgPushLog;
import com.dhcc.sdc.api.service.ISdsReceiveDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * API接收数据接口
 *
 * @ClassName: ApiReceiveDataController
 * @Description: TODO
 * @author: zx
 * @date: 2023年06月02日 上午10:04:55
 */
@RestController
@RequestMapping("${sys.apiPath}")
@Api("接收数据接口")
@Async
public class SdsReceiveDataController extends BaseApiController {
    @Autowired
    private ISdsReceiveDataService sdsReceiveDataService;
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ICdcDataReceiveLogService dataReceiveLogService;
    private static final ExecutorService analysisDataPools = Executors.newFixedThreadPool(10);//分析数据线程池

    /**
     * 消息接收接口
     */
    @PostMapping("/SdsMessageServer")
    @ApiOperation(value = "消息接收接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "string", name = "action", value = "消息代码", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "string", name = "message", value = "数据", required = true)
    })
    @ResponseBody
    public R messageServer(@RequestParam(required = true) String action, @RequestParam(required = true) String message) {
        analysisDataPools.execute(new saveData(action,message));//分析数据
        return R.ok().put("msg","同步成功");
    }
    public void saveMsg(String action,String message)throws Exception{
        switch (action) {
            case "EPISODE_INFO"://就诊信息
                sdsReceiveDataService.handlePaAdmInfo(message);
                break;
            case "DIAG_INFO"://诊断信息
                sdsReceiveDataService.handlDiagInfo(message);
                break;
            case "ORDER_INFO"://医嘱信息
                sdsReceiveDataService.handleOrderInfo(message);
                break;
            case "TRANSFER_INFO"://转科信息
                sdsReceiveDataService.handleTransferInfo(message);
                break;
            case "EMR_INFO"://电子病历信息
                sdsReceiveDataService.handleEmrInfo(message);
                break;
            case "LABVIST_INFO"://检验样本信息
                sdsReceiveDataService.handleLabVisitInfo(message);
                break;
            case "LAB_INFO"://检验报告信息
                sdsReceiveDataService.handleLabReportInfo(message);
                break;
            case "EXAM_INFO"://检查报告信息
                sdsReceiveDataService.handleExamReportInfo(message);
                break;
            case "SURG_INFO"://手术信息
                sdsReceiveDataService.handleSurgeryInfo(message);
                break;
            case "SIGN_INFO"://体征信息
                sdsReceiveDataService.handleSignInfo(message);
                break;
            case "ENR_INFO"://护理信息
                sdsReceiveDataService.handleNurEmrInfo(message);
                break;
            case "EXP_INFO"://费用信息
                break;
            case "HOSP_INFO"://医院信息
                break;
            case "LOC_INFO"://科室信息
                break;
            case "USER_INFO"://用户信息
                break;
            case "LAB_DICT_INFO"://检验条目信息
                break;
            case "ORDER_DICT_INFO"://医嘱字典信息
                break;
            case "ORDER_GRP_INFO"://医嘱套信息
                break;
            case "ALLERGY_INFO"://过敏信息
                break;
        }
    }
    public class saveData extends Thread {
        private final String action;
        private final String message;

        public saveData(String action, String message) {
            this.action = action;
            this.message = message;
        }

        public void run() {
            try {
                String dataReceiveLog = CdcConfigCache.me().getValueByKey("SDS", "DataReceiveLog");
                if("1".equals(dataReceiveLog)){
                    DataReceiveLogEntity log = new DataReceiveLogEntity();
                    log.setAction(action);
                    log.setMessage(message.toString());
                    log.setReceiveTime(new Date());
                    dataReceiveLogService.insert(log);
                }
                //消费
                saveMsg(action, message);
            } catch (Exception e) {
                e.printStackTrace();
                DataReceiveLogEntity log = new DataReceiveLogEntity();
                log.setError(e.getMessage());
                log.setAction(action);
                log.setMessage(message.toString());
                log.setReceiveTime(new Date());
                dataReceiveLogService.insert(log);
            }
        }
    }
}
